package com.facebook.imagepipeline.producers;

import android.net.Uri;
import com.facebook.common.internal.VisibleForTesting;
import com.facebook.imagepipeline.image.EncodedImage;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class HttpUrlConnectionNetworkFetcher extends BaseNetworkFetcher<FetchState>
{
  public static final int HTTP_PERMANENT_REDIRECT = 308;
  public static final int HTTP_TEMPORARY_REDIRECT = 307;
  private static final int MAX_REDIRECTS = 5;
  private static final int NUM_NETWORK_THREADS = 3;
  private final ExecutorService mExecutorService;

  public HttpUrlConnectionNetworkFetcher()
  {
    this(Executors.newFixedThreadPool(3));
  }

  @VisibleForTesting
  HttpUrlConnectionNetworkFetcher(ExecutorService paramExecutorService)
  {
    this.mExecutorService = paramExecutorService;
  }

  private HttpURLConnection downloadFrom(Uri paramUri, int paramInt)
    throws IOException
  {
    HttpURLConnection localHttpURLConnection = openConnectionTo(paramUri);
    int i = localHttpURLConnection.getResponseCode();
    if (isHttpSuccess(i))
      return localHttpURLConnection;
    if (isHttpRedirect(i))
    {
      String str1 = localHttpURLConnection.getHeaderField("Location");
      localHttpURLConnection.disconnect();
      if (str1 == null);
      for (Uri localUri = null; ; localUri = Uri.parse(str1))
      {
        String str2 = paramUri.getScheme();
        if ((paramInt <= 0) || (localUri == null) || (localUri.getScheme().equals(str2)))
          break;
        return downloadFrom(localUri, paramInt - 1);
      }
      Object[] arrayOfObject3;
      if (paramInt == 0)
      {
        arrayOfObject3 = new Object[1];
        arrayOfObject3[0] = paramUri.toString();
      }
      Object[] arrayOfObject2;
      for (String str3 = error("URL %s follows too many redirects", arrayOfObject3); ; str3 = error("URL %s returned %d without a valid redirect", arrayOfObject2))
      {
        throw new IOException(str3);
        arrayOfObject2 = new Object[2];
        arrayOfObject2[0] = paramUri.toString();
        arrayOfObject2[1] = Integer.valueOf(i);
      }
    }
    localHttpURLConnection.disconnect();
    Object[] arrayOfObject1 = new Object[2];
    arrayOfObject1[0] = paramUri.toString();
    arrayOfObject1[1] = Integer.valueOf(i);
    throw new IOException(String.format("Image URL %s returned HTTP code %d", arrayOfObject1));
  }

  private static String error(String paramString, Object[] paramArrayOfObject)
  {
    return String.format(Locale.getDefault(), paramString, paramArrayOfObject);
  }

  private static boolean isHttpRedirect(int paramInt)
  {
    switch (paramInt)
    {
    case 304:
    case 305:
    case 306:
    default:
      return false;
    case 300:
    case 301:
    case 302:
    case 303:
    case 307:
    case 308:
    }
    return true;
  }

  private static boolean isHttpSuccess(int paramInt)
  {
    return (paramInt >= 200) && (paramInt < 300);
  }

  @VisibleForTesting
  static HttpURLConnection openConnectionTo(Uri paramUri)
    throws IOException
  {
    return (HttpURLConnection)new URL(paramUri.toString()).openConnection();
  }

  public FetchState createFetchState(Consumer<EncodedImage> paramConsumer, ProducerContext paramProducerContext)
  {
    return new FetchState(paramConsumer, paramProducerContext);
  }

  public void fetch(final FetchState paramFetchState, final NetworkFetcher.Callback paramCallback)
  {
    final Future localFuture = this.mExecutorService.submit(new Runnable()
    {
      public void run()
      {
        HttpUrlConnectionNetworkFetcher.this.fetchSync(paramFetchState, paramCallback);
      }
    });
    paramFetchState.getContext().addCallbacks(new BaseProducerContextCallbacks()
    {
      public void onCancellationRequested()
      {
        if (localFuture.cancel(false))
          paramCallback.onCancellation();
      }
    });
  }

  @VisibleForTesting
  void fetchSync(FetchState paramFetchState, NetworkFetcher.Callback paramCallback)
  {
    HttpURLConnection localHttpURLConnection = null;
    try
    {
      localHttpURLConnection = downloadFrom(paramFetchState.getUri(), 5);
      if (localHttpURLConnection != null)
        paramCallback.onResponse(localHttpURLConnection.getInputStream(), -1);
      return;
    }
    catch (IOException localIOException)
    {
      paramCallback.onFailure(localIOException);
      return;
    }
    finally
    {
      if (localHttpURLConnection != null)
        localHttpURLConnection.disconnect();
    }
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.facebook.imagepipeline.producers.HttpUrlConnectionNetworkFetcher
 * JD-Core Version:    0.6.2
 */